using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Drawing;
using System.Drawing.Imaging;
using MULENetworking;

namespace MULEServer
{
    /// <summary>
    /// Implementation of our remote object interfaces for access through the network.
    /// </summary>
    [Serializable]
    public class Player : MarshalByRefObject, IPlayer
    {
        #region IPlayer Variables

        private string playerName,bidtype, playerMuleType = "";
        private int playerNumber, playerEnergy, playerFood, playerCrystite, playerSmithore, 
            playerNetworth, playerAvatar, playerMoney, playerGoodsworth, playerLandworth, 
            playerPlace, playerState, playerBid;
        private Color playerColor;
        private bool playerHasMule, playerIsActive;
        public ArrayList plotsOwned;
        private PointF playerLocation, playerDestination;
        protected Bitmap avatarPic, mulePic;

        #endregion

        //ALSO NEED WAY TO SPECIFY AVATAR AND COLOR???

        // @param level // 0 = beginner, 1 = standard, 2 = tourney
        public Player(string myName, int level, int avatar, Color color) // add avatar to this
        {
            playerName = myName;
            playerNumber = -1;
            playerEnergy = -1;
            playerFood = -1;
            playerCrystite = -1;
            playerSmithore = -1;
            playerNetworth = -1;
            playerGoodsworth = -1;
            playerLandworth = -1;
            playerAvatar = avatar;
            playerPlace = -1;
            playerHasMule = false;
            playerIsActive = false;
            playerBid = 0;
            mulePic = new Bitmap("mulegreen.bmp");
            playerColor = color;
            plotsOwned = new ArrayList();

            switch (level)
            {
                case 0:
                    food = 8;
                    energy = 4;
                    money = 1000;
                    break;
                case 1:
                    food = 4;
                    energy = 2;
                    money = 1000;
                    break;
                case 2:
                    food = 4;
                    energy = 2;
                    money = 600;
                    break;
            }
        }

        #region IPlayer Members

        public string name
        {
            get
            {
                return playerName;
            }
            set
            {
                playerName = value;
            }
        }

        public int number
        {
            get
            {
                return playerNumber;
            }
            set
            {
                playerNumber = value;
            }
        }

        public System.Drawing.Color color
        {
            get
            {
                return playerColor;
            }
            set
            {
                playerColor = value;
                if (color==Color.Blue)
                    avatarPic = new Bitmap("blueAvatars.bmp");
                if (color==Color.Red)
                    avatarPic = new Bitmap("redAvatars.bmp");
                if (color==Color.Purple)
                    avatarPic = new Bitmap("purpleAvatars.bmp");
                if (color == Color.Orange)
                    avatarPic = new Bitmap("orangeAvatars.bmp");
                if (color == Color.White)
                    avatarPic = new Bitmap("whiteAvatars.bmp");
            }
        }

        public int energy
        {
            get
            {
                return playerEnergy;
            }
            set
            {
                playerEnergy = value;
            }
        }

        public int food
        {
            get
            {
                return playerFood;
            }
            set
            {
                playerFood = value;
            }
        }

        public int smithore
        {
            get
            {
                return playerSmithore;
            }
            set
            {
                playerSmithore = value;
            }
        }

        public int crystite
        {
            get
            {
                return playerCrystite;
            }
            set
            {
                playerCrystite = value;
            }
        }

        public bool hasMule
        {
            get
            {
                return playerHasMule;
            }
            set
            {
                playerHasMule = value;
            }
        }

        public bool myTurn
        {
            get
            {
                return playerIsActive;
            }
            set
            {
                playerIsActive = value;
            }
        }

        public int networth
        {
            get
            {
                return playerNetworth;
            }
            set
            {
                playerNetworth = value;
            }
        }

        public int avatar
        {
            get
            {
                return playerAvatar;
            }
            set
            {
                playerAvatar = value;
            }
        }

        public int money
        {
            get
            {
                return playerMoney;
            }
            set
            {
                playerMoney = value;
            }
        }

        public string muleType
        {
            get
            {
                return playerMuleType;
            }
            set
            {
                playerMuleType = value;
            }
        }

        public PointF location
        {
            get { return playerLocation; }
            set { playerLocation = value; }
        }

        public int goodsworth
        {
            get
            {
                return playerGoodsworth;
            }
            set
            {
                playerGoodsworth = value;
            }
        }

        public int landworth
        {
            get
            {
                return playerLandworth;
            }
            set
            {
                playerLandworth = value;
            }
        }

        public int place
        {
            get
            {
                return playerPlace;
            }
            set
            {
                playerPlace = value;
            }
        }

        public int PlayerState
        {
            get
            {
                return playerState;
            }
            set
            {
                playerState = value;
            }
        }

        public int Bid
        {
            get
            {
                return playerBid;
            }
            set
            {
                playerBid = value;
            }
        }

        #endregion

        #region IPlayer Members


        public string BidType
        {
            get
            {
                return bidtype;
            }
            set
            {
                bidtype = value;
            }
        }

        #endregion

        #region IPlayer Members


        public PointF destination
        {
            get
            {
                return playerDestination;
            }
            set
            {
                playerDestination = value;
            }
        }

        #endregion

        //NEED AVATAR PICTURE

        public void Draw(Graphics g)
        {

            float x = playerLocation.X;
            float y = playerLocation.Y;
            float diffy=1;
            float diffx=1;

            if (playerDestination.X > x)
            {
                if ((playerDestination.X - x) > 2)
                    diffx = 2;
                x += diffx;
            }
            else if (playerDestination.X < x)
            {
                if ((playerDestination.X - x) < 2)
                    diffx = 2;
                x -= diffx;
            }
            if (playerDestination.Y > y)
            {
                if ((playerDestination.Y - y) > 2)
                    diffy = 2;
                y += diffy;
            }
            else if (playerDestination.Y < y)
            {
                if ((playerDestination.Y - y) < 2)
                    diffy = 2;
                y -= diffy;
            }

            playerLocation = new PointF(x, y);
            Rectangle position = new Rectangle((int)playerLocation.X,(int)playerLocation.Y,25,35);
            avatarPic.MakeTransparent(Color.FromArgb(0,255,0));
            g.DrawImage(avatarPic, position, 0, 28 * playerAvatar, 20, 28, GraphicsUnit.Pixel);
            if (hasMule)
            {
                mulePic.MakeTransparent(Color.FromArgb(0,255,0));
                g.DrawImage(mulePic,playerLocation.X+20,playerLocation.Y+5,40,40);
            }
        }

        public void DrawIcon(Graphics g, Point p, Size s)
        {
            Brush strbrush = new SolidBrush(Color.Black);
            Font strfont = new Font("Arial", 11);
            Rectangle position = new Rectangle(p.X+5, p.Y + 10, s.Width,s.Height);
            avatarPic.MakeTransparent(Color.FromArgb(0, 255, 0));
            g.DrawImage(avatarPic, position, 80, 28 * playerAvatar, 20, 28, GraphicsUnit.Pixel);
            g.DrawString(playerName, strfont, strbrush, p);
        }
    }
}
